//

// (  )
//

package com.tools.acceleratecore.logic.accelerate.process.impl;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import com.tools.acceleratecore.AccelerateCoreFactory;
import com.tools.acceleratecore.logic.accelerate.process.intf.IProcessItem;
import com.tools.acceleratecore.logic.accelerate.process.intf.IProcessKiller;
import com.tools.acceleratecore.logic.accelerate.process.intf.IProcessMgr;
import com.tools.acceleratecore.logic.accelerate.process.intf.IProcessMgrListener;
import com.tools.acceleratecore.xlib.XLibFactory;
import com.tools.acceleratecore.xlib.xlib.impl.XObserver;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTask;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTaskListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ProcessMgr extends XObserver<IProcessMgrListener> implements IProcessMgr {
    private Context mContext = null;
    private boolean mInited = false;
    private IXThreadTask mIXThreadTaskInit = null;
    private IXThreadTask mIXThreadTaskScanProcess = null;
    private List<ProcessItem> listProcessItem = null;

    public ProcessMgr() {
        this.mContext = AccelerateCoreFactory.getApplication();
        this._init();
    }

    private void _init() {
        this.mIXThreadTaskInit = (IXThreadTask)XLibFactory.getInstance().createInstance(IXThreadTask.class);
        this.mIXThreadTaskScanProcess = (IXThreadTask)XLibFactory.getInstance().createInstance(IXThreadTask.class);
        this.listProcessItem = new ArrayList();
    }

    public boolean init() {
        if(this.mInited) {
            return false;
        } else {
            this.mInited = true;
            this.mIXThreadTaskInit.start(new IXThreadTaskListener() {
                public void onThreadWork() {
                }

                public void onThreadComplete() {
                    synchronized(ProcessMgr.this.mListListener) {
                        Iterator var3 = ProcessMgr.this.getListListener().iterator();

                        while(var3.hasNext()) {
                            IProcessMgrListener listener = (IProcessMgrListener)var3.next();
                            listener.onProcessMgrInitComplete();
                        }

                    }
                }
            });
            return true;
        }
    }

    public void startScanProcessAsyn() {
        this.mIXThreadTaskScanProcess.start(new IXThreadTaskListener() {
            public void onThreadWork() {
                ProcessMgr.this.startScanProcessSyn();
            }

            public void onThreadComplete() {
                synchronized(ProcessMgr.this.mListListener) {
                    Iterator var3 = ProcessMgr.this.getListListener().iterator();

                    while(var3.hasNext()) {
                        IProcessMgrListener listener = (IProcessMgrListener)var3.next();
                        listener.onStartScanProcessAsynComplete();
                    }

                }
            }
        });
    }

    public void startScanProcessSyn() {
        this.scanProcess();
    }

    private void scanProcess() {
        this.listProcessItem.clear();
        ActivityManager am = (ActivityManager)this.mContext.getSystemService("activity");
        List<RunningAppProcessInfo> listRunningAppProcessInfo = am.getRunningAppProcesses();
        Iterator var4 = listRunningAppProcessInfo.iterator();

        while(var4.hasNext()) {
            RunningAppProcessInfo info = (RunningAppProcessInfo)var4.next();
            if(info != null && info.pkgList.length != 0) {
                int nPID = info.pid;
                int nUID = info.uid;
                String strProcessName = info.processName;
                String strPackageName = info.pkgList[0];
                ProcessItem item = (ProcessItem)AccelerateCoreFactory.getInstance().createInstance(IProcessItem.class);
                item.setProcessID(nPID);
                item.setUserID(nUID);
                item.setProcessName(strProcessName);
                item.setProcessImportance(info.importance);
                item.setAppPackageName(strPackageName);
                ProcessKillerNormal processKillerNormal = (ProcessKillerNormal)AccelerateCoreFactory.getInstance().createInstance(IProcessKiller.class, ProcessKillerNormal.class);
                processKillerNormal.setPackageName(strPackageName);
                item.setProcessKiller(processKillerNormal);
                this.listProcessItem.add(item);
            }
        }

    }

    public int getProcessItemCount() {
        return this.listProcessItem.size();
    }

    public IProcessItem queryProcessItem(int nIndex) {
        return (IProcessItem)this.listProcessItem.get(nIndex);
    }
}
